import json
from log.log import Log
from jsonpath import jsonpath
from ERRO.erro import asert
from base.Interface import Request_processing
log=Log()
sql=Request_processing()
class Aseert:
    """断言处理"""

    def assert_value(self,expected_value,actual_value):
        """
        :param expected_value: 期望值列表
        :param actual_value: 实际值
        :return: Node
        """
        for i in expected_value:
            try:
                if list(i.keys())[0]=='ep':
                    """等于断言"""
                    if hasattr(actual_value, i.get('ep')[0]):
                        """动态处理想要的返回情况"""
                        value = getattr(actual_value, i.get('ep')[0])
                        assert i.get('ep')[1] == value
                    else:
                        self.value = jsonpath(json.loads(actual_value.text), i.get('ep')[0])
                        assert self.value[0] == i.get('ep')[1]
                    log.log_assert(i.get('ep')[1])
                else:
                    """不等于断言"""
                    if hasattr(actual_value, i.get('epx')[0]):
                        """动态处理想要的返回情况"""
                        value = getattr(actual_value, i.get('epx')[0])
                        assert i.get('epx')[1] != value
                    else:
                        self.value = jsonpath(json.loads(actual_value.text), i.get('epx')[0])
                        assert self.value[0] != i.get('epx')[1]
                    log.log_assert(i.get('epx')[1])
            except AssertionError:
                # log.log.info("断言失败，期望值:{},实际值:{}".format(i.get('ep')[1], actual_value.text))
                raise AssertionError(asert(i.get('ep')[1], actual_value.text))
            except TypeError:
                raise TypeError("实际返回字段当中({})没有'{}',无法完成断言".format(actual_value.text,i.get('ep')[0]))
    def assert_sql(self,value,expected_value,number=0):

        if value!=():
            if expected_value in value[number]:
                log.sql_assert()
                assert True
            else:
                log.sql_assert_f()
                assert False
        else:
            assert expected_value==[]









